import subprocess
import os.path
import os,sys,shutil
import re

resPath = ""
svnPath = ""


deleteUrls = ""

resAndSvnPaths = [["","","files_"]]

filesmd5 = ""
zipMaxversion = 0

dict = {}
delNum = 0
delArrNum = 0
delArr = []

def makeDirIfnot(dir):
	if not os.path.exists(dir):
		os.makedirs(dir)

def deleteifexist(path,svnPathArg,rootPath):
	global deleteUrls
	global delNum
	global delArr
	global delArrNum

	if os.path.isfile(path):
		final = path.replace(rootPath,svnPathArg)
		final = final.replace("\\","//")
		print(u"删除文件"+final)
		deleteUrls +=" "+final
		delNum +=1
		print(u"delfile",delNum)
		if(delNum>230):
			delArrNum +=1
			delNum = 0
			delArr.append(deleteUrls)
			deleteUrls = ""


 def checkFile(path,file,svnPathArg,rootPath,filterFile):
 	url = path + "\\"+file
 	#填了文件过滤后就不检查下级目录
 	if filterFile:
 		if url.find(filterFile)<=-1:
 			return
 		if os.path.isdir(url):
 			return
 	if os.path.isfile(url):
 		#files_特殊处理
 		if fileterFile is "files_":
 			return
 		#zip特殊处理
 		if filterFile is "zip":
 			return

 		addIdx = file.rfind(".")
 		jpg = file[addIdx:]
 		if(addIdx>-1):
 			name = file[:addIdx]
 			versonIdx = name.rfind("_")
 			length = len(name)

 			if(versionIdx>-1 and name[versionIdx+1:].isdigit() and length - versionIdx == 7):
 				versonStr = name[versionIdx+1:]
 				realname = name[:versionIdx]

 				verson = int(versonStr)
 				fileUrl = path+"\\"+realname + jpg
 				maxVerson = dict.setdefault(fileUrl,verson)

 				if (verson<maxVerson):
 					deleteifexist(url,svnPathArg,rootPath)
 				if maxVerson<verson:
 					dict[fileUrl]=verson
 					deleteifexist(path +"\\"+realname+"_"+('%06d' % maxVerson) + jpg,svnPathArg,rootPath)
 	if os.path.isdir(url) and file.find(".svn") <= -1:
 		checkDir(url,svnPathArg,rootPath,filterFile)

def checkDir(path,svnPathArg,rootPath,filterFile):
	files = os.listdir(path)
	for file in files:
		checkFile(path,file,svnPathArg,rootPath,filterFile)

def commit():
	subprocess.Popen([r"powershell","svn delete -m 'auto deleteFiles'"+deleteUrls])
	print("commit finish")

def checkDirDel(res,svn,filterFile):
	global delArr
	global deleteUrls
	delArr = []
	deleteUrls = ""
	checkDir(res,svn,res,filterFile)
	delArr.append(deleteUrls)
	print("del_len",len(delArr))
	for delItem in delArr:
		if(delItem!=""):
			subprocess.Popen([r"powershell","svn delete -m 'auto deleteFiles'"+delItem])

for path in resAndSvnPaths:
	checkDirDel(resPath+path[0],svnPath+path[1],path[2])


print("prefect!!!")
os.system("pause")



